package com.cmccsi.service.impl;



import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.cmccsi.entity.qxuserDto;
import com.cmccsi.mapper.qxuserDtoMapper;
import com.cmccsi.service.QxUserService;
import com.cmccsi.utils.MybatisUtil;

public class QxUserServiceImpl implements QxUserService {
	
	private SqlSessionFactory sessionFactory = MybatisUtil.getInstance();
	private QxUserRedisServiceImpl userRedis=new QxUserRedisServiceImpl();
	
	@Override
	public Boolean save(qxuserDto user,int optType) {
		SqlSession session = sessionFactory.openSession();
		try {
			
			qxuserDtoMapper userMapper = session.getMapper(qxuserDtoMapper.class);			
			qxuserDto usrHaved= userMapper.selectByModileOrUsernameAndDomainId(user.getDomainid(),user.getUserName(),user.getMobilePhone());
		  if(usrHaved==null)
			{			  
				userMapper.insert(user);
				session.commit();
				userRedis.save(user, 0);
				return true;
			}
			else
			{
				usrHaved.setDomainid(user.getDomainid());
				usrHaved.setUserName(user.getUserName());
				usrHaved.setMobilePhone(user.getMobilePhone());
				usrHaved.setCyloginflag(user.getCyloginflag());
				userRedis.save(user, 1);
				return update(usrHaved);
			}		  
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return false;
	}

	@Override
	public Boolean update(qxuserDto user) {
		SqlSession session = sessionFactory.openSession();
		try {
			qxuserDtoMapper userMapper = session.getMapper(qxuserDtoMapper.class);
			userMapper.updateByPrimaryKey(user);
			session.commit();
			userRedis.update(user);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return false;
	}

	@Override
	public qxuserDto find(Long id) {
		qxuserDto user = null;
		SqlSession session = sessionFactory.openSession();
		try {
			qxuserDtoMapper userMapper = session.getMapper(qxuserDtoMapper.class);
			user = userMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return user;
	}

	@Override
	public Boolean delete(Long id) {
		SqlSession session = sessionFactory.openSession();
		try {
			qxuserDtoMapper userMapper = session.getMapper(qxuserDtoMapper.class);
			userMapper.deleteByPrimaryKey(id);
			session.commit();
			userRedis.delete(id);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return false;
	}
	
	
	@Override
	public Boolean deleteByMobile(String Mobile) {
		SqlSession session = sessionFactory.openSession();
		try {
			qxuserDtoMapper userMapper = session.getMapper(qxuserDtoMapper.class);
			qxuserDto user=userMapper.selectByModile(Mobile);
			if(user!=null)
			{
				return delete(user.getId());
			}	
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		
		return false;
	}
	
	@Override
	public Boolean deleteByDomainId(String DomainId) {
		SqlSession session = sessionFactory.openSession();
		try {
			qxuserDtoMapper userMapper = session.getMapper(qxuserDtoMapper.class);
			userMapper.deleteByDomainId(DomainId);
			session.commit();
			//暂不处理清空所有部门人员的redis数据缓存的状态
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return false;
	}
}
